package com.jett.JFtpServer.JFtpServer;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.net.NetUtil;
import cn.hutool.log.Log;
import com.jett.JFtpServer.Enum.RetMsg;
import org.apache.ftpserver.ConnectionConfig;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ipfilter.SessionFilter;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfiguration;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;

import java.io.File;

public class JFtpServer {
    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory listenerFactory = new ListenerFactory();
    private static final Log log = Log.get();

    public static JFtpServer create() {
        return new JFtpServer();
    }

    public JFtpServer() {
    }

    public FtpServerFactory getServerFactory() {
        return this.serverFactory;
    }

    public JFtpServer setConnectionConfig(ConnectionConfig connectionConfig) {
        this.serverFactory.setConnectionConfig(connectionConfig);
        return this;
    }

    public ListenerFactory getListenerFactory() {
        return this.listenerFactory;
    }

    public JFtpServer setPort(int port) {
        Assert.isTrue(NetUtil.isValidPort(port), "Invalid port!", new Object[0]);
        this.listenerFactory.setPort(port);
        return this;
    }

    public UserManager getUserManager() {
        return this.serverFactory.getUserManager();
    }

    public JFtpServer addUser(User user) {
        try {
            this.getUserManager().save(user);
            return this;
        } catch (FtpException var3) {
            throw new cn.hutool.extra.ftp.FtpException(var3);
        }
    }

    public JFtpServer delUser(String userName) {
        try {
            this.getUserManager().delete(userName);
            return this;
        } catch (FtpException var3) {
            throw new cn.hutool.extra.ftp.FtpException(var3);
        }
    }

    public JFtpServer setSsl(SslConfiguration ssl) {
        this.listenerFactory.setSslConfiguration(ssl);
        this.listenerFactory.setImplicitSsl(true);
        return this;
    }

    public JFtpServer setSsl(File keystoreFile, String password) {
        SslConfigurationFactory sslFactory = new SslConfigurationFactory();
        sslFactory.setKeystoreFile(keystoreFile);
        sslFactory.setKeystorePassword(password);
        return this.setSsl(sslFactory.createSslConfiguration());
    }

    public JFtpServer setUserManager(UserManager userManager) {
        this.serverFactory.setUserManager(userManager);
        return this;
    }

    public JFtpServer setUsersConfig(File propertiesFile) {
        PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
        userManagerFactory.setFile(propertiesFile);
        return this.setUserManager(userManagerFactory.createUserManager());
    }

    public JFtpServer addFtplet(String name, Ftplet ftplet) {
        this.serverFactory.getFtplets().put(name, ftplet);
        return this;
    }

    public void start() {
        this.serverFactory.addListener("default", this.listenerFactory.createListener());
        try {
            this.serverFactory.createServer().start();
        } catch (FtpException var2) {
            throw new cn.hutool.extra.ftp.FtpException(var2);
        }
    }

    public void start(SessionFilter sessionFilter) {
        if (sessionFilter == null) {
            log.error("sessionFilter为空!", new Object[0]);
            throw new RuntimeException(RetMsg.Err0005.getRetMsg());
        } else {
            this.listenerFactory.setSessionFilter(sessionFilter);
            this.start();
        }
    }
}
